#!/usr/bin/rc

if (~ `{uname -a} *Debian*) {
  os_flavor=debian
} else {
  os_flavor=arch
}

########################
## package management ##
########################

fn whats { # search for software
  if (~ $os_flavor debian) {
    apt-cache search $* | grep --color'='always '^\S\+' | less -R
  } else {
    pacman --color always -Ss $* | less -R
  }
}

fn update { # update the system
  if (~ $os_flavor debian) {
    sudo apt-get update && sudo apt-get upgrade
  } else {
    sudo pacman -Syu
  }
}

fn gimme { # install software
  if (~ $os_flavor debian) {
    update && sudo apt-get install $*
  } else {
    update && sudo pacman -S $*
  }
}

fn drop { # remove software
  if (~ $os_flavor debian) {
    sudo apt-get remove $* && sudo apt-get autoremove
  } else {
    sudo pacman -Rs $*
  }
}

if (~ $os_flavor debian) {
  fn makedeb { # build a debian package from source ONLY ON DEBIAN
    dpkg-buildpackage -rfakeroot -uc -b
  }
}

fn howdoi { # print executable files given by a package
  if (~ $os_flavor debian) {
    f = () for (f in `{dpkg -L $1}) {
      if (test -x $f && ! test -d $f) {
        echo $f
      }
    }
  } else {
    f = () for (f in `{pacman -Ql $1 | cut -f2 -d' '}) {
      if (test -x $f && ! test -d $f) {
        echo $f
      }
    }
  }
}

fn about { # print man-pages given by a package
  if (~ $os_flavor debian) {
    basename -a `{dpkg -L $1 | grep '^/usr/share/man/.*gz'} | sort
  } else {
    basename -a `{pacman -Ql $1 | cut -f2 -d' ' | grep '^/usr/share/man/.*gz'} | sort
  }
}

#######################
## rc list functions ##
#######################

fn pop { # pop the head from a list
  name=$1
  *=$$name
  popped=$1
  shift
  $name=$*
  echo $popped
}

fn push { # (list x ...) push x onto list
  name=$1 {
    shift
    x = () for (x in $*) {
      $name=($x $$name)
    }
  }
}

fn join { # (list sep) join a list by sep
  sep=$2 {
    *=$$1
    while (true) {
      echo -n $1
      shift
      if (~ $#* 0) {
        break
      } else {
        echo -n $sep
      }
    }
  }
}

#########################
## Shorthand functions ##
#########################

fn l { # ls --color=always
  ls --color'='always $*
}

fn cls { # clear ; ls
  clear
  l
}

fn attach { # attach to an existing tmux session, or create a new one
  tmux attach || tmux
}

fn jmp { # jump into a directory, creating it if needed
  mkdir -p $1 && cd $1
}

fn pne { # perl -ne while (/.../g) { print ...\n }
  x = $2 {
    if (~ $x ()) { x = '$1' }
    perl -ne 'while (/'$1'/g) { print "'$x'\n" }'
  }
}

###########
## papes ##
###########

fn watchimg { # watch for new files in the current directory, and display them via $*
  while(true) {
    f=`{inotifywait -e create .} {
      clear
      $* $f(3)
    }
  }
}

fn getwalls { # (query, [page]) download wallpapers
  page=1 {
    if (~ $#* 2) {
      page=$2
    }

    i = () for (i in `{wh $1 $page}) {
      echo -n `{basename $i}
      curl $i -Os
      echo ' ... DONE'
    }
  }
}

###################
## Introspection ##
###################

fn my_functions { # list functions
  @ {
    awk_script = '
    /^##/ {
      if (banner == 0) { print "" };
      print "\x1b[34m" $0 "\x1b[0m";
      banner=1
    }

    /^fn (.*) \{ # (.*)$/ {
      banner=0;
      print("\x1b[31m" $2 ":\x1b[0m" substr($0, index($0, "#") + 1));
    }
    '

    awk $awk_script $home/.rc.d/0010-functions.rc
  }
}